package guowei.gulielasticsearch.controller;

import guowei.gulielasticsearch.service.SearchEsService;
import guowei.gulielasticsearch.vo.SearchParam;
import guowei.gulielasticsearch.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
public class SearchController {

    @Autowired
    private SearchEsService searchEsService;

    /**
     * 根据页面传来的商品信息，来进行es检索
     * @param searchParam
     * @return
     */
    @GetMapping("/list.html")
    public String listPage(SearchParam searchParam, Model model, HttpServletRequest request){

//为搜索参数传入查询条件，以便取消面包屑导航时，替换url
        String queryString = request.getQueryString();
        searchParam.set_queryString(queryString);

        SearchResult searchResult = searchEsService.getInfosByParams(searchParam);
        model.addAttribute("result",searchResult);
        return "list";
    }
    @ResponseBody
    @GetMapping({"/","/index"})
    public String shouye(){
        return "请访问list.html页面";
    }
}
